/*******************************************************************************
 * Copyright (c) 2011 Erik Byström.
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package tazadum.razzmatazz;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.gl2.GLUgl2;
import javax.swing.JFrame;

import tazadum.engine.Engine;
import tazadum.razzmatazz.scene.SolidBackgroundScene;
import tazadum.razzmatazz.scene.WireframeLandscapeScene;
import tazadum.util.ColorUtil;

import com.jogamp.opengl.util.Animator;

/**
 * @author Erik Byström
 * 
 */
public class Razzmatazz implements GLEventListener {

	static {
		// Not working properly in Linux amd64
		// GLProfile.initSingleton(true);
	}

	public static void main(final String[] args) {
		final int WIDTH = 640;
		final int HEIGHT = 480;

		final GLProfile profile = GLProfile.get(GLProfile.GL2);
		final GLCapabilities capabilities = new GLCapabilities(profile);

		final GLCanvas glcanvas = new GLCanvas(capabilities);
		glcanvas.addGLEventListener(new Razzmatazz());
		glcanvas.setSize(WIDTH, HEIGHT);

		final JFrame frame = new JFrame("razzmatazz");
		frame.getContentPane().add(glcanvas);

		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(final WindowEvent ev) {
				frame.dispose();
				System.exit(0);
			}
		});

		frame.setSize(frame.getContentPane().getPreferredSize());
		frame.setResizable(false);
		frame.setVisible(true);

		final Animator animator = new Animator(glcanvas);
		animator.setRunAsFastAsPossible(false);
		animator.start();
	}

	private final Engine engine;

	/**
	 * Starting point of razzmatazz
	 */
	public Razzmatazz() {
		engine = new Engine();
		engine.loadingScene(new SolidBackgroundScene(ColorUtil.toVector(0xffffe4)));
		engine.add(0, new SolidBackgroundScene(ColorUtil.toVector(0xffffff)));
		engine.add(1, new WireframeLandscapeScene());

		engine.setDebug(true);

		// try {
		// engine.setMusicStream(new FileInputStream("ingemar - indie train.mp3"));
		// } catch (final FileNotFoundException e) {
		// throw new RuntimeException(e);
		// }
	}

	@Override
	public void init(final GLAutoDrawable drawable) {
		engine.init(drawable);

		final GL2 gl = drawable.getGL().getGL2();
		final GLUgl2 glu = new GLUgl2();
		final int width = drawable.getWidth();
		final int height = drawable.getHeight();

		gl.glClearDepth(1);
		gl.glEnable(GL.GL_DEPTH_TEST);
		gl.glDepthFunc(GL.GL_LEQUAL);
		gl.glEnable(GL.GL_BLEND);

		gl.glMatrixMode(GL2.GL_PROJECTION);
		gl.glLoadIdentity();
		glu.gluPerspective(45.0f, width / height, 0.1f, 1000.0f);

		gl.glMatrixMode(GL2.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	@Override
	public void display(final GLAutoDrawable drawable) {
		engine.display(drawable);
	}

	@Override
	public void dispose(final GLAutoDrawable drawable) {
		engine.shutdown();
	}

	@Override
	public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
		// should never be called because of no reshape
	}
}
